<template>
  <basic-dialog
    width="60%"
    :title="dialogTitle"
    :visible="visible"
    :showFooter="false"
    :handleClose="handleCloseDialog"
  >
    <BasicForm
      :colNumOfRow="2"
      :isScalingBtn="false"
      :hasQueryPermission="false"
      :formColumns="formItemList"
      :dataSource="formData"
      :handleSearchForm="handleQueryForm"
      @getFormData="getFormData"
    />
    <BasicTable
      showActionCol
      :showSelectCol="false"
      :loading="tableInfo.loading"
      :columns="tableInfo.columns"
      :dataSource="tableInfo.list"
      :pagination="pagination"
      :pageSizes="pageSizes"
      :tableHeight="480"
      :actionRender="renderTableBtnAction"
      @pageChange="handlePageChange"
      @pageSizeChange="handlePageSizeChange"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicForm from '@/components/BasicForm';
import BasicTable from '@/components/BasicTable';
import basePage from '@/mixins/commodity/basePage';
import { getTableAction } from '@/utils/getBtnsRender';
import { querySupplierService } from '@/api/commodity/common';
export default {
  mixins: [basePage],
  components: {
    BasicDialog,
    BasicForm,
    BasicTable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    dialogTitle: {
      type: String,
      default: '选择供应商'
    },
    isAuth: {
      type: Number
    }
  },
  data () {
    return {
      formData: { supplyName: '' }, // 计量单位名称
      formItemList: [{ label: '供应商', prop: 'supplyName', type: 'input' }],
      tableInfo: {
        list: [],
        columns: [
          { title: '供应商编号', key: 'supplierNo' },
          { title: '供应商名称', key: 'supplierName' },
          { title: '主联系人', key: 'contractMan' },
          { title: '联系电话', key: 'contractTel' },
          { title: '生产地址', key: 'address' }
        ],
        loading: false
      }
    }
  },
  watch: {
    visible (newVal) {
      newVal && this.getTableData();
    }
  },
  methods: {
    // 渲染表格按钮
    renderTableBtnAction (h, { row }) {
      const actionList = [{
        label: '选择',
        method: this.handleChooseSupplier
      }];
      return getTableAction({ h, row, actionList })
    },
    // 获取表单查询入参
    getSearchFormParams () {
      const { nowPage, pageShow } = this.pagination;
      const params = {
        nowPage,
        pageShow,
        searchParam: JSON.stringify([
          { name: 'supplierName', value: this.formData.supplyName },
          { name: 'supplierName_q', value: 'Like' }
        ]),
      }
      if (this.isAuth) {
        params.isAuth = this.isAuth
      }
      return params
    },
    // 获取表格数据
    async getTableData () {
      const data = this.getSearchFormParams();
      const res = await querySupplierService(
        data,
        state => { this.tableInfo.loading = state; }
      );
      if (res?.retData) {
        this.tableInfo.list = res.retData?.results ?? [];
        this.pagination.totalNum = res.retData?.totalNum ?? 0;
      }
    },
    // 选择供应商
    handleChooseSupplier (row) {
      this.$emit('getSupplierInfo', {
        ...row,
        supplierCode: row.supplierNo
      });
      this.handleCloseDialog();
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
  }
}
</script>
<style lang="less" scoped>
/deep/ .el-dialog__header {
  border-bottom: 1px solid #e8eaec;
}
/* 分页样式 */
.pagination_wrap {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
